What is hyperid?
The hyperid npm package is a fast and efficient library for generating unique identifiers. It is designed to be lightweight and performant, making it suitable for use in high-throughput environments.
What are hyperid's main functionalities?
Generate Unique IDs
This feature allows you to generate unique identifiers. The `hyperid` function creates an instance, and calling this instance generates a unique ID.
const hyperid = require('hyperid');
const instance = hyperid();
const id = instance();
console.log(id);
Generate IDs with Fixed Prefix
This feature allows you to generate unique identifiers with a fixed length. This can be useful when you need IDs of a consistent size.
const hyperid = require('hyperid');
const instance = hyperid({ fixedLength: true });
const id = instance();
console.log(id);
Decode IDs
This feature allows you to decode a generated ID back into its constituent parts. This can be useful for debugging or for extracting information from the ID.
const hyperid = require('hyperid');
const instance = hyperid();
const id = instance();
const decoded = instance.decode(id);
console.log(decoded);
Other packages similar to hyperid
uuid
The uuid package is a popular library for generating UUIDs (Universally Unique Identifiers). It supports multiple versions of UUIDs, including v1, v3, v4, and v5. Compared to hyperid, uuid is more versatile in terms of the types of UUIDs it can generate, but it may not be as performant in high-throughput scenarios.
nanoid
The nanoid package is a small, secure, URL-friendly unique string ID generator. It is designed to be fast and efficient, similar to hyperid. However, nanoid focuses on generating shorter, URL-friendly IDs, whereas hyperid provides more flexibility in terms of ID structure and decoding capabilities.
shortid
The shortid package is a library for generating short, non-sequential unique IDs. It is designed to be simple and easy to use. Compared to hyperid, shortid generates shorter IDs, but it does not offer the same level of performance or the ability to decode IDs.
hyperid
Uber-fast unique id generation, for Node.js and the browser.
Here are the benchmarks:
crypto.randomUUID x 12,969,725 ops/sec ±0.88% (91 runs sampled)
hashids process.hrtime x 419,350 ops/sec ±0.66% (94 runs sampled)
hashids counter x 819,049 ops/sec ±0.58% (93 runs sampled)
shortid x 40,820 ops/sec ±2.49% (87 runs sampled)
crypto.random x 372,773 ops/sec ±2.39% (84 runs sampled)
nid x 1,614,450 ops/sec ±0.38% (93 runs sampled)
uuid.v4 x 1,446,051 ops/sec ±0.60% (98 runs sampled)
napiRsUuid.v4 x 8,676,151 ops/sec ±0.49% (97 runs sampled)
uuid.v1 x 2,051,072 ops/sec ±0.15% (99 runs sampled)
nanoid x 4,293,733 ops/sec ±0.31% (97 runs sampled)
hyperid - variable length x 25,937,129 ops/sec ±1.48% (91 runs sampled)
hyperid - fixed length x 24,970,478 ops/sec ±1.48% (92 runs sampled)
hyperid - fixed length, url safe x 25,856,735 ops/sec ±1.93% (92 runs sampled)
Fastest is hyperid - variable length,hyperid - fixed length, url safe
Slowest is shortid
Note: Benchmark run with Intel(R) Core(TM) i7-7700 CPU @ 3.60GHz and Node.js v22.0.0
As you can see the native crypto.randomUUID
is almost as fast as hyperid
on Node.js v16, but not on v14.
Install
npm i hyperid --save
Example
'use strict'
const hyperid = require('hyperid')
const instance = hyperid()
const id = instance()
console.log(id)
console.log(instance())
console.log(hyperid.decode(id))
console.log(hyperid.decode(instance()))
API
hyperid([fixedLength || options])
Returns a function to generate unique ids.
The function can accept one of the following parameters:
fixedLength: Boolean
If fixedLength is true
the function will always generate an id
that is 33 characters in length, by default fixedLength
is false
.options: Object
If { fixedLength: true }
is passed in, the function will always generate an id
that is 33 characters in length, by default fixedLength
is false
.
If { urlSafe: true }
is passed in, the function will generate url safe ids according to RFC4648.
If { startFrom: <int> }
is passed in, the first counter will start from that
number, which must be between 0 and 2147483647. Fractions are discarded, only the
integer part matters.
If { maxInt: <int> }
is passed in, the uuid will be re-generated once the maxInt is reached. The lesser the maxInt, higher the performance because of SMI (a V8 optimization).
instance()
Returns an unique id.
instance.uuid
The uuid used to generate the ids, it will change over time.
If maxInt
is provided in options, then it will regenerated every maxInt
, else it will be regenerated every Math.pow(2, 31) - 1
to keep the integer a SMI (a V8 optimization).
hyperid.decode(id, [options])
Decode the unique id into its two components, a uuid
and a counter.
If you are generating url safe ids, you must pass { urlSafe: true }
as option.
It returns:
{
uuid: '049b7020-c787-41bf-a1d2-a97612c11418',
count: 1
}
This is aliased as instance.decode
.
License
MIT